#include <stdio.h>
#include <stdlib.h>	/* exit() */
#include <pthread.h>

/*
 * pthread_exit 参数的不正确使用
 */

struct foo 
{
	int a, b, c, d;
};

void printfoo(const char * s, const struct foo * fp)
{
	printf(s);
	printf("	structure at 0x%x\n", (unsigned)fp);
	printf("	foo.a = %d\n",	fp->a);
	printf("	foo.b = %d\n",	fp->b);
	printf("	foo.c = %d\n",	fp->c);
	printf("	foo.d = %d\n",	fp->d);
}

void * thr_fn1(void * arg)
{
	struct foo foo = {1, 2, 3, 4};

	printfoo("thread 1 : \n", &foo);
	pthread_exit((void *) &foo);	// 终止当前线程
}

void * thr_fn2(void * arg)
{
	printf("thread 2 : ID is %d\n", pthread_self());	// 查询线程自身线程标识号
	pthread_exit ((void *) 0);		// 终止当前线程
}

int main(void)
{
	int err;
	pthread_t tid1, tid2;
	struct foo *fp;

	pthread_create(&tid1, NULL, thr_fn1, NULL);

	err = pthread_join(tid1, (void * ) &fp);	// 阻塞当前的线程，直到 tid1 线程运行结束 
	if (err != 0)
		printf("can't join with thread 1 : %s'n", strerror(err));
	sleep(1);

	printf("parent starting second thread.\n");
	pthread_create(&tid2, NULL, thr_fn2, NULL);
	sleep(1);
	printfoo("parent:\n", fp);

	exit(0);
}

/*
 
thread 1 : 
        structure at 0xb7772380
        foo.a = 1
        foo.b = 2
        foo.c = 3
        foo.d = 4
parent starting second thread.
thread 2 : ID is -1216926864
parent:
        structure at 0xb7772380
        foo.a = 0
        foo.b = 1214996468
        foo.c = 1214664604
        foo.d = 1214988912

*/
